package georeduy.dao;

import java.util.List;

import georeduy.clases.Admin;
import georeduy.dao.interfaces.IAdminDAO;
import georeduy.utilidades.ExceptionManager;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;



@Stateless
public class AdminDAO implements IAdminDAO
{
	private static final String BUSCAR_ADMIN_POR_LOGIN_PASSWORD = "select u from Admin u where upper(u.user) = :login  and upper(u.password) = :password";
  
    @PersistenceContext(unitName="Geored.uy-JPA")
    private EntityManager em;

    public AdminDAO() {
    }

    public Object queryByRange(String jpqlStmt, int firstResult,
                               int maxResults) {
        Query query = em.createQuery(jpqlStmt);
        if (firstResult > 0) {
            query = query.setFirstResult(firstResult);
        }
        if (maxResults > 0) {
            query = query.setMaxResults(maxResults);
        }

        return query.getResultList();
    }

     public Admin persistAdmin(Admin admin) {
        em.persist(admin);
        return admin;
    }
    public Admin mergeAdmin(Admin admin) {
        return em.merge(admin);
    }

    public void removeAdmin(Admin admin) {
        admin = em.find(Admin.class, admin.getId());
        em.remove(admin);
    }
    
    @SuppressWarnings("unchecked")
	@TransactionAttribute(TransactionAttributeType.REQUIRED)
	public List<Admin> buscarUsuarioPorLoginPassword(String login ,String password)
	{
		try
		{
			Query query = em.createQuery(BUSCAR_ADMIN_POR_LOGIN_PASSWORD);
			query.setParameter("login" ,login );
			query.setParameter("password" ,password );
			return query.getResultList();
		}
		catch (Throwable ex)
		{
			throw ExceptionManager.process(ex);
		}

	}

	

}
